/*
 * @lc app=leetcode.cn id=826 lang=cpp
 *
 * [826] 安排工作以达到最大收益
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    int maxProfitAssignment(vector<int> &difficulty, vector<int> &profit, vector<int> &worker)
    {
        priority_queue<pair<int, int>> pq;
        for (int i = 0; i < difficulty.size(); i++)
            pq.push(make_pair(profit[i], difficulty[i]));

        multiset<int> s;
        for (int i = 0; i < worker.size(); i++)
            s.insert(worker[i]);

        int ans = 0;
        while (!pq.empty() && !s.empty())
        {
            auto item = pq.top();
            pq.pop();
            int pro = item.first;
            int diff = item.second;

            auto it = s.begin();
            while ((it = s.lower_bound(diff)) != s.end())
            {
                ans += pro;
                s.erase(it);
            }
        }

        return ans;
    }
};

// int main()
// {
//     vector<int> diff = {7, 20, 68};
//     vector<int> pro = {26, 28, 57};
//     vector<int> worker = {71, 20, 71};
//     cout << Solution().maxProfitAssignment(diff, pro, worker) << endl;
//     return 0;
// }

// @lc code=end
